package com.work;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class experimentTwo extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        Label label1 = new Label("显示字母：");
        TextField label2 = new TextField("A");
        label2.setEditable(false);
        label2.setPrefWidth(30.0);
        Label label3 = new Label("输入所显示的字母（回车）");
        TextField label4 = new TextField();
        Label label5 = new Label("得分");
        Label label6 = new Label("0");
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    char str;
                    str = (char) (Math.random() * 26 + 'a');
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    String s = String.valueOf(str);
                    label2.setText(s);
                }
            }
        }).start();

        label4.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent e) {
                if (e.getCode() == KeyCode.ENTER) {
                    String t1 = label2.getText();
                    String t2 = label4.getText();
                    String t3 = label5.getText();
                    int i = Integer.parseInt(t3);
                    if (t1.equals(t2)) {
                        i ++ ;
                        label5.setText(String.valueOf(i));

                    }
                }
                label4.clear();
            }
        });

        FlowPane flowPane = new FlowPane();
        flowPane.getChildren().addAll(label1, label2, label3, label4, label5, label6);
        Scene scene = new Scene(flowPane);
        stage.setScene(scene);
        stage.setWidth(900);
        stage.show();
    }
}